package com.ruoyi.web.controller.common;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.ImmutableList;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.ruoyi.web.controller.common.RobotConstant.*;
import static com.ruoyi.web.controller.common.RobotFeatureEnum.LUNAR;
import static java.util.concurrent.TimeUnit.*;

public class MatchUtil {

    public static final List<String> HUANG_LI_DAY = ImmutableList.of("大前天","前天","昨天","今天","明天","后天","大后天");
    /**
     *
     * @param url
     * @param suffixes  "jpeg|jpg|png"
     * @return
     */
    public static String getMatch(String url,String suffixes){
        if (StrUtil.isBlank(suffixes)) {
            suffixes="jpeg|jpg|png";
        }
        Pattern pat=Pattern.compile("[\\w]+[\\.]("+suffixes+")");//正则判断
        Matcher mc=pat.matcher(url);//条件匹配
        while(mc.find()) {
            String fileName= mc.group();//截取文件名后缀名
            return fileName;
        }
        return "";
    }

    public static String getMatch(String url){
        String suffixes="jpeg|jpg|png|mp4";
        return MatchUtil.getMatch(url,suffixes);
    }

    public static String getInputMsg(String msg ,RobotFeatureEnum type){
        String s = "";
        if (msg.contains(AT_EG)) {
            s = StrUtil.replace(msg, AT_EG,StrUtil.EMPTY);
        } else if (msg.contains(AT_EG2)) {
            s = StrUtil.replace(msg, AT_EG2,StrUtil.EMPTY);
        } else if (msg.contains(AT_EG3)) {
            s = StrUtil.replace(msg, AT_EG3,StrUtil.EMPTY);
        }
        if (StrUtil.isBlank(s)) {
            return StrUtil.EMPTY;
        }
        if (ObjectUtil.isNotNull(type)) {
            return StrUtil.replace(s, type.codeName(),StrUtil.EMPTY);
        }
        return s.replace(" ","");
    }

    public static String getInputMsg(String msg){
        return getInputMsg(msg,null);
    }

    public static String getHuangLiDay(String msg){
        String s = getInputMsg(msg,LUNAR);
        if (StrUtil.isBlank(s)) {
            return StrUtil.EMPTY;
        }
        s = s.trim();
        switch (s){
            case "大前天":
                return DateUtil.offsetDay(new Date(),-3).toDateStr();
            case "前天":
                return DateUtil.offsetDay(new Date(),-2).toDateStr();
            case "昨天":
                return DateUtil.yesterday().toDateStr();
            case "今天":
                return DateUtil.formatDate(new Date());
            case "明天":
                return DateUtil.tomorrow().toDateStr();
            case "后天":
                return DateUtil.offsetDay(new Date(),2).toDateStr();
            case "大后天":
                return DateUtil.offsetDay(new Date(),3).toDateStr();
        }
        return s;
    }

    public static String unitToChinese(TimeUnit unit) {
        if (SECONDS == unit) {
            return "秒钟";
        }
        if (MINUTES == unit) {
            return "分钟";
        }
        if (HOURS == unit) {
            return "小时";
        }
        if (DAYS == unit) {
            return "天";
        }
        return "";
    }

    /***
     *  有人回答对
     * @param cacheObject
     * @return
     */
    public static String getCorrectResponse(TianEntity cacheObject){
        return getCorrectResponse(cacheObject,null);
    }

    /**
     *
     * @param cacheObject
     * @param robot 不为空，说明没人回答对。
     * @return
     */
    public static String getCorrectResponse(TianEntity cacheObject , String robot){
        StringBuilder sb = new StringBuilder();
        sb.append(" \n");
        sb.append("【"+cacheObject.getRoomTask()+"】");
        sb.append(" \n");
        if (StrUtil.isBlank(robot)) {
            sb.append(CORRECT_PREFIX.get(RandomUtil.randomInt(0,CORRECT_PREFIX.size())));
        }
        sb.append("问题");
        sb.append(" \n");
        sb.append(cacheObject.getQuest());
        sb.append("的答案就是：");
        sb.append(cacheObject.getResult());
        sb.append(" \n");
        if (StrUtil.isNotBlank(cacheObject.getReason())) {
            sb.append("。原因是：");
            sb.append(cacheObject.getReason());
            sb.append(" \n");
        }
        return sb.toString();
    }
}
